package com.shuang.graph10;

import java.util.*;
//邻接表存储 深搜或者 广搜
public class Main {
    public static List<List<Integer>> adjList = new ArrayList<>();

    public static void dfs(boolean[] visited, int key) {
        //终止条件
        if (visited[key]) {
            return;
        }
        visited[key] = true;
        //得到传入节点连接的所有节点
        List<Integer> nextKeys = adjList.get(key);
        for (int nextKey : nextKeys) {
            dfs(visited, nextKey);
        }
    }

    public static void bfs(boolean[] visited, int key) {
        Queue<Integer> queue = new LinkedList<Integer>();
        queue.add(key);
        visited[key] = true;
        while (!queue.isEmpty()) {
            int curKey = queue.poll();
            List<Integer> list = adjList.get(curKey);
            for (int nextKey : list) {
                if (!visited[nextKey]) {
                    queue.add(nextKey);
                    visited[nextKey] = true;
                }
            }
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        for (int i = 0; i <= n; i++) {
            adjList.add(new LinkedList<>());
        } //Initialization
        for (int i = 1; i <= k; i++) {
            int s = sc.nextInt();
            int t = sc.nextInt();
            adjList.get(s).add(t);
        } //构造邻接表
        //标记数组
        boolean[] visited = new boolean[n + 1];

        //深搜
        // dfs(visited, 1);

        //广搜
        bfs(visited, 1);

        for (int i = 1; i <= n; i++) {
            if (!visited[i]) {
                System.out.println(-1);
                return;
            }
        }
        System.out.println(1);
    }
}
